
program drop _all

/* THIS PROGRAM IMPLEMENTS LOGNORMAL MODEL-SURVIVAL REGRESSIONS IN TABLE 6 (cols. 3-4); IC-STATA ver. 10.1 
   NOTE: All the source and results files are saved at single directory: C:\RESTAT\   */


program define DUR
clear all
set more off
set memory 80m
set matsize 800
use C:\RESTAT\data1.dta, replace
log using C:\RESTAT\Tab6_survival.log, replace 



                       tsset firm year
                       sort primaryu

/* MERGE DATA with HIGH/LOW_FDI INDICATORS */
                      merge primaryu using C:\RESTAT\FDI_CNB.dta

                      display" DROP 11 INDUST WITHOUT FOREIGN PRESENCE"
                      drop if low_fdi==.


/* RESTRICT SAMPLE TO HIGH FDI (there is 65 high and 65 low-FDI indust.) */
                   
                   display" KEEP high_fdi"
                   keep if high_fdi==1 


#delimit ;           
 
                    display" In case of 2 observations IndG missing, because no data there on sales in year 1994";
                    display"Replace these by IndG==0";
                    replace IndG=0 if primaryu==175 & year==1994;      


/* CREATE DUMMY=1 WHENEVER DOM ENTRY RATE>0 (ANALOGICALLY WITH FORentry var (=STATIC) */
                   

           /* gen OTHER_ENTRANTS1 */
                   display" GENERATE OTHER_ENTRANTS1 BY SUBTRACTING 1 IF THE FIRM IS OF AGE=1";
                   gen OTHER_ENTRANTS1=ENTRANTS1;
                   replace OTHER_ENTRANTS1=ENTRANTS1-1 if age==1; 
                   replace OTHER_ENTRANTS1=0 if OTHER_ENTRANTS1<0;
                

          /* gen dummy for DOMENTRY */
                    gen DOMENTRY=1 if  OTHER_ENTRANTS1>0;
                    replace DOMENTRY=0 if OTHER_ENTRANTS1==0;
                                                    



/* KEEP ONLY OBS W/ NON-MISSING REGRESSORS */                  
                    keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 


              /* gen firm level means */
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);               
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);
                    by firm: egen MIN_ageT=min(ageT);

                                                                  
                    display" COUNT INDUSTRIES BEFORE RE-GROUPING -- HAVE 65 HIGH-FDI INDUST.";
                    sort primaryu;
                    count if primaryu[_n]~=primaryu[_n-1];

                      tsset firm year;

/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST */

                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination " */
                    /* so now yearly dummies are: 94,97, 98, 99,00 */             

                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);
 


/* ADDITIONAL REPLACEMENTS NEEDED IF WE DROP 11 INDUST WITHOUT FDI */
                  replace primaryu=359 if primaryu==351;
                  replace primaryu=339 if primaryu==332;


/* ADDITIONAL REPLACEMENTS FOR high-fdi */                           
                   replace primaryu=541 if primaryu==554; 
                   replace primaryu=308 if primaryu==317;
                   replace primaryu=738 if primaryu==735;

                   
                   display" FOR THE SIMILAR REASONS REPLACE ageT WITH 12 IF ageT==13 ";
                   replace ageT=12 if ageT==13;  


 

 /* SET DATA FOR DURATION */
                    
                    /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ; 
                  



/* ************************* LOGNORMAL: Tab6 (col.3) - INDUST W/ HIGH-FDI STOCK ******************************* */


               xi:streg FG STATIC DOMENTRY sales sales2 ES FORdirect intang gap ESgap D89 solvency 
                          year98-year00 i.primaryu reg1-reg7 IndG 
                          avFG avsales avsales2 avES avFORdirect avintang 
                          avgap avESgap avsolvency,dist(lnormal) cluster(firm) tr;

  

          

/* **************************** REPEAT ANALYSIS BUT NOW RESTRICT THE SAMPLE FOR INDUST low_fdi=1 ************* */

clear;
use C:\RESTAT\data1.dta, replace;


                       tsset firm year;
                       sort primaryu;

/* MERGE DATA with HIGH/LOW_FDI INDICATORS */
                     merge primaryu using C:\RESTAT\FDI_CNB.dta;  

                    display" DROP 11 INDUST WITHOUT FOREIGN PRESENCE";
                    drop if low_fdi==.;              
 
                    display" In case of 2 observations IndG missing, because no data there on sales in year 1994";
                    display"Replace these 2 obs by IndG==0";
                    replace IndG=0 if primaryu==175 & year==1994;      


/* CREATE DUMMY=1 WHENEVER DOM ENTRY RATE>0 (ANALOGICALLY WITH FORentry var (=STATIC) */
                   

           /* gen OTHER_ENTRANTS1 */
                   display" GENERATE OTHER_ENTRANTS1 BY SUBTRACTING 1 IF THE FIRM IS OF AGE=1";
                   gen OTHER_ENTRANTS1=ENTRANTS1;
                   replace OTHER_ENTRANTS1=ENTRANTS1-1 if age==1;
                   replace OTHER_ENTRANTS1=0 if OTHER_ENTRANTS1<0;
                

          /* gen dummy for DOMENTRY */
                    gen DOMENTRY=1 if  OTHER_ENTRANTS1>0;
                    replace DOMENTRY=0 if OTHER_ENTRANTS1==0;
                                                    



/* KEEP ONLY OBS W/ NON-MISSING REGRESSORS */                  
                    keep if EI~=. & FG~=. & IndG~=. & ageT~=. & sales~=. & AS~=.& ES~=.& FORdirect~=. &
                          intang~=. & ESgap~=. & gap~=. & solvency~=. ; 


              /* gen firm level means */
                    sort firm ;
                    by firm: egen avsales=mean(sales);
                    by firm: egen avsales2=mean(sales2);
                    by firm: egen avESgap=mean(ESgap);
                    by firm: egen avFG=mean(FG);               
                    by firm: egen avageT=mean(ageT);
                    by firm: egen avageT2=mean(ageT2);
                    by firm: egen avAS=mean(AS);
                    by firm: egen avES=mean(ES);
                    by firm: egen avFORdirect=mean(FORdirect);
                    by firm: egen avintang=mean(intang);
                    by firm: egen avgap=mean(gap);
                    by firm: egen avsolvency=mean(solvency);
                    by firm: egen MIN_ageT=min(ageT);

                    display" COUNT INDUSTRIES BEFORE RE-GROUPING -- HAVE 65 LOW-FDI INDUST.";
                    sort primaryu;
                    count if primaryu[_n]~=primaryu[_n-1] & low_fdi==1;

                      tsset firm year;

                                                 

/* TO AVOID PERFECT FAILURE/SUCCESS DETERMINATION PROBLEM RE-GROUP DUMMIES - APPLY RE-GROUPINGS FROM TAB4 FIRST */

                    display" GROUP YEAR DUMMIES INTO 2 YEARS (94+95), (96+97), WHERE PERFECT FAIULRE DETERM. PROBLEM";
                    replace year94=1 if (year==1994|year==1995);
                    replace year97=1 if (year==1996|year==1997);
                    replace year00=1 if (year==2000|year==2001);
                    drop year95 year96 ; /* 2 years when perfect failure determination " */
                    /* so now yearly dummies are: 94,97, 98, 99,00 */             

                   replace primaryu=16 if US2==16;
                   replace primaryu=17 if US2==17;
                   replace primaryu=203 if (primaryu==204|primaryu==205);
                   replace primaryu=208 if primaryu==209;
                   replace primaryu=22 if (US2==22|US2==23);
                   replace primaryu=24 if  US2==24;
                   replace primaryu=25 if  US2==25;
                   replace primaryu=26 if (US2==26|US2==27|US2==28);
                   replace primaryu=317 if (primaryu==322|primaryu==325|primaryu==326|
                                           primaryu==327|primaryu==328);
                   replace primaryu=332 if primaryu==333;
                   replace primaryu=343 if primaryu==344;
                   replace primaryu=345 if primaryu==346;
                   replace primaryu=351 if primaryu==352;
                   replace primaryu=361 if primaryu==362;
                   replace primaryu=363 if primaryu==364;
                   replace primaryu=374 if primaryu==375;
                   replace primaryu=38 if (US2==38|US2==39);
                   replace primaryu=47 if (primaryu==472|primaryu==481|primaryu==491|primaryu==493);
                   replace primaryu=505 if (primaryu==506|primaryu==507|primaryu==508);
                   replace primaryu=515 if primaryu==516;
                   replace primaryu=519 if primaryu==523;
                   replace primaryu=554 if (primaryu==571|primaryu==581);
                   replace primaryu=591 if (primaryu==593|primaryu==594);
                   replace primaryu=621 if primaryu==628;
                   replace primaryu=721 if primaryu==729;
                   replace primaryu=735 if primaryu==737;
                   replace primaryu=792 if (primaryu==794|primaryu==799);
 
               


/* RESTRICT SAMPLE TO LOW FDI (there is 65 high and 65 low-FDI indust.) */                   
                   display" KEEP low_fdi";
                   keep if low_fdi==1 ;             
 

/* ADDITIONAL REPLACEMENTS FOR low-fdi */                                               
                   replace primaryu=331 if primaryu==317;                
                   replace primaryu=25 if primaryu==26;              
                   replace primaryu=701 if primaryu==721;
                   replace primaryu=354 if primaryu==351;
                   replace primaryu=339 if primaryu==332;


                   replace year98=1 if year==1997; /* notice no exits in 3 yrs: 1995-96-97, so merge 96&97 w/ 98 */
                   drop year97; /* year dummies: 94, 98, 99, 00, so replace in regressions: year97 w/ year98-00 */


 /* SET DATA FOR DURATION */
                    
                    /* USING AGE_T */
                    gen MAT=MIN_ageT-1 ;
                    stset ageT, id(firm) failure(EI==1) entry(MAT) ; 
                  


/* ************************* LOGNORMAL: Tab6 (col.4) - INDUST W/ LOW-FDI STOCK ******************************* */


               xi:streg FG STATIC DOMENTRY sales sales2 ES FORdirect intang gap ESgap D89 solvency 
                          year98-year00 i.primaryu reg1-reg7 IndG 
                          avFG avsales avsales2 avES avFORdirect avintang 
                          avgap avESgap avsolvency,dist(lnormal) cluster(firm) tr;



#delimit cr
log close
end
